package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.UserContact;
import com.example.zbh.dsliao.utils.PreferenceHelper;

import java.util.List;

/**
 * Created by zbh on 15/12/2.
 */
public class RemoveMemberAdapter extends RecyclerView.Adapter<RemoveMemberAdapter.RemoveViewHolder> {
    private List<String> list;
    private Context context;

    public RemoveMemberAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<String> list) {
        this.list = list;
        notifyDataSetChanged();

    }

    @Override
    public RemoveViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyclerview_groupdetail_item, null);
        return new RemoveViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RemoveViewHolder holder, int position) {
        if (isLength()) {
            if (position == 0) {
                holder.image_huan.setVisibility(View.VISIBLE);
                String avater = PreferenceHelper.getInstance().getThreeCurrentUserAvatar();
                if (avater != null) {
                    Glide.with(context).load(avater)
                            .placeholder(R.drawable.dl_default_avatar)
                            .error(R.drawable.dl_default_avatar)
                            .into(holder.image_header);
                } else {
                    holder.image_header.setImageResource(R.drawable.dl_default_avatar);
                }
            }
            String userName = list.get(position);
            UserContact userContact = DatabaseHelper.getInstance().queryContactPeopleBynickName(userName);
            if (userContact != null) {
                String avatar = userContact.getAvatar();
                if (avatar != null){
                    Glide.with(context).load(avatar)
                            .placeholder(R.drawable.dl_default_avatar)
                            .error(R.drawable.dl_default_avatar)
                            .into(holder.image_header);
                }
            }else {
                holder.image_header.setImageResource(R.drawable.dl_default_avatar);
            }

            holder.user_name.setText(userName);
            if (position > 0) {
                holder.image_j.setVisibility(View.VISIBLE);
            }

        }

    }


    public boolean isLength() {
        return list != null && list.size() > 0;
    }

    @Override
    public int getItemCount() {
        return isLength() ? list.size() : 0;
    }


    class RemoveViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView image_header, image_j;
        private TextView user_name;
        private LinearLayout groupMember;
        private ImageView image_huan;


        public RemoveViewHolder(View itemView) {
            super(itemView);
            image_header = (ImageView) itemView.findViewById(R.id.image_header);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            groupMember = (LinearLayout) itemView.findViewById(R.id.groupMember);
            image_huan = (ImageView) itemView.findViewById(R.id.image_huan);
            image_j = (ImageView) itemView.findViewById(R.id.image_j);
            groupMember.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.groupMember:
                    if (listener != null) {
                        listener.onRemoveMemberItemClick(list, getLayoutPosition());
                    }
                    break;
            }
        }
    }

    public interface OnRemoveMemberItemClickListener {
        void onRemoveMemberItemClick(List<String> list, int position);
    }

    private OnRemoveMemberItemClickListener listener;

    public void setOnRemoveMemberItemClickListener(OnRemoveMemberItemClickListener listener) {
        this.listener = listener;
    }


}
